package web;

import entity.Pet;
import entity.PetOwner;
import entity.PetStore;
import service.PetOwnerService;
import service.PetService;
import service.PetStoreService;
import service.impl.PetOwnerServiceImpl;
import service.impl.PetServiceImpl;
import service.impl.PetStoreServiceImpl;

import java.util.List;
import java.util.Scanner;


/*定义程序启动的方法*/
public class PetShop {
    PetService petService = new PetServiceImpl();
    PetOwnerService petOwnerService = new PetOwnerServiceImpl();
    PetStoreService petStoreService = new PetStoreServiceImpl();
    Scanner scanner = new Scanner(System.in);
    List<PetStore> petStoreList;//所有商店
    //启动
    public void strat() throws Exception {
        /*系统启动*/
        //从数据库获取所有宠物
        System.out.println("系统启动");
        System.out.println("***************************************");
        System.out.println("所有宠物 从 Oracle醒来");
        System.out.println("序号\t宠物名称");
        List<Pet> petList = petService.getPetList();
        for (Pet pet : petList) {
            System.out.println(pet.getId() + "\t" + pet.getName());
        }

        //从数据库获取所有主人
        System.out.println("***************************************");
        System.out.println("所有主人 从 Oracle醒来");
        System.out.println("序号\t主人名称");
        List<PetOwner> petOwnerList = petOwnerService.getPetOwnerList();
        for (PetOwner petOwner : petOwnerList) {
            System.out.println(petOwner.getId() + "\t" + petOwner.getName());
        }

        //从数据库获取所有商店
        System.out.println("***************************************");
        System.out.println("所有商店 从 Oracle醒来");
        System.out.println("序号\t商店名称");
       petStoreList = petStoreService.getPetStoreList();
        for (PetStore petStore : petStoreList) {
            System.out.println(petStore.getId() + "\t" + petStore.getName());
        }
        Login();

    }

    //登入
    public void Login() throws Exception {
        System.out.print("请选择登入模式：1、宠物主人登入 2、宠物商店登入");
        int choose = scanner.nextInt();
        if (choose == 1) {
            //宠物主人登入
            PetOwner petOwner = new PetOwner();
            System.out.print("请输入用户ID：");
            int id = scanner.nextInt();
            System.out.print("请输入用户名：");
            String name = scanner.next();
            petOwner.setId(id);
            petOwner.setName(name);
            PetOwner petOwner1 = petOwnerService.login(petOwner);
            System.out.println("登入成功，您可以购买和卖出宠物，如果您想购买宠物请输入1，如果您想卖出宠物请输入2\r\n1:购买宠物\r\n2:卖出宠物");
            int number = scanner.nextInt();
            if (number == 1) {
                //主人买
                ownerBuy(petOwner1);
            } else if (number == 2) {
                //主人卖
                ownerSell(petOwner1);
            }

        } else {
            //宠物商店登入
            PetStore petStore = new PetStore();
            System.out.print("请输入用户ID：");
            int id = scanner.nextInt();
            System.out.print("请输入用户名：");
            String name = scanner.next();
            petStore.setId(id);
            petStore.setName(name);
            PetStore login = petStoreService.login(petStore);
            // System.out.println("欢迎店主\t"+login.getName());
        }
    }

    //购买
    public void ownerBuy(PetOwner petOwner1) throws Exception {
        System.out.println("······请输入选择要购买的范围：只输入选择项的序号·····\r\n1:购买库存宠物\r\n2:购买新培育宠物");
        int count = scanner.nextInt();
        System.out.println("序号\t\t宠物名称\t\t类型\t\t\t元宝数");
        List<Pet> storePetList = petService.getStorePetList();
        for (Pet pet : storePetList) {
            System.out.println(pet.getId() + "\t\t" + pet.getName() + "\t\t\t" + pet.getTypeName() + "\t\t\t" + pet.getPrice());
        }
        System.out.println("······请选择购买哪一个宠物，并输入选择项的序号······");
        int id = scanner.nextInt() - 1;
        System.out.println(storePetList.get(0));

        boolean buy = petService.getBuy(storePetList.get(id), petOwner1);
        if (buy) {
            System.out.println("插入记录成功");
        } else {
            System.out.println("插入记录失败");
        }
    }

    //卖出
    public void ownerSell(PetOwner petOwner1) throws Exception {
        System.out.println("------------我的宠物列表--------");
        List<Pet> list = petService.ownerPet(petOwner1);
        System.out.println("序号\t\t宠物名称\t\t类型");
        for (int i = 0; i < list.size(); i++) {

            System.out.println((i+1) + "\t\t" + list.get(i).getName() + "\t\t\t" + list.get(i).getTypeName());
        }
        if(list.size()==0){
            System.out.println("您当前未养宠物，是否需要购买？\ty  是 ，\tn  否");
            String flag = scanner.next();

        }
        System.out.println("---------请选择要出售的宠物序号-------");
        int i = scanner.nextInt();
        System.out.println("-------您要卖出的宠物信息如下-----------");
        System.out.println("名字：\t"+list.get(i-1).getName()+"\t\t\t类型：\t"+ list.get(i-1).getTypeName());
        System.out.println("宠物商店信息:");
        System.out.println("序号\t\t名称");

        petStoreList = petStoreService.getPetStoreList();
        for (PetStore petStore : petStoreList) {
            System.out.println(petStore.getId() + "\t" + petStore.getName());
        }

        System.out.println("---------请选择要出售到的宠物店的序号-------");
        int num = scanner.nextInt()-1;

        boolean sell= petService.sellPet(list.get(i-1),petStoreList.get(num),petOwner1);

        if (sell) {
            System.out.println("插入记录成功");
        } else {
            System.out.println("插入记录失败");
        }
    }
}
